package com.firefly.mobile.app.util;


/**
 * @author ydw
 * 
 */
public class RSAUtil {
	/**对方公钥 */
	private static String partnerPubkey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8DciU6xIV+91qbGUijLvh4wLnb9QZGiEsu72whbPh4/bZKwr4TW3TfaqqQtIOmHMUhE85UY+oeUr+vwzh5cONw6FGEySiFTm8H/Pf8ZffTe5k6IPSRf//JJ7paryf0l5BHTu90oMX4I4Pn0YUJHydsqMmMzVm0KA5oNE4jp2rkQIDAQAB";
	
	private static String selfPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCIVXrnK+4hVdq4dDhtzY7kueQtBohoaAkC2Gl+0BnyHKWEOunUBWS1yirmfFY9eAZFaKuioxS00R6aPMqgtzMDQDYQzyLCvfGNNnRA3WBCs+SU2CBybvjHErZVzvfhOKBNKDVTEzTcQwOGfUkmvbQ7XcWtxIz5cOZnj5EOww7fnQIDAQAB";
	/**本方私钥 */
	private static String selfPrivateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIhVeucr7iFV2rh0OG3NjuS55C0GiGhoCQLYaX7QGfIcpYQ66dQFZLXKKuZ8Vj14BkVoq6KjFLTRHpo8yqC3MwNANhDPIsK98Y02dEDdYEKz5JTYIHJu+McStlXO9+E4oE0oNVMTNNxDA4Z9SSa9tDtdxa3EjPlw5mePkQ7DDt+dAgMBAAECgYBIj75vRZljkq9r1yelgW+ZWzsPn9eAQkbxhb94aEg27XpGy5hiaVB11RlKPUklRDmKPbCESmod5uNz0gFC0t7J/1kB2kHr6W/Mdfj54jqHDvDwVjA/fnDqZLrYYR9R8ihj0SeMCGJnXQqkV2nr7+yxMhIz47tjlXFEzOqzy/gdnQJBAPvW4z05yvx/wFPNIu6dAJSfYCM3iSFmCRglPsVpN/jt89fbzRbF8fRqg1ZPUoYdew566LCFl7M3RopJVBpdozcCQQCKlhTVtEkWIlbSGYABYv6mKmLiXWFLZdWiwEIrl25XL+bEMIhIpAYa06DE83pbTAePkPKrib0ySn+3q+AKRKXLAkEA3rQ6AbOw2LeUeccHZCF9GPETkDEVWG6mDi8pomOYts6eez4J0GC9xZnHiTTeUnGRl82DJk8NRtPT0ohkEmjJ4wJAf+c1wWoLjg8RPATm2+DGjwtNigeO1SxeOD/ikiLHZlZCT81O6mPfcoDgvwTLZ1lLHRg0r1aHM9QH/bb2JePncwJBAK0ow0XEQxJF16JU8KSpCbPOdWy1a6XJ4vOGM7pk+KvswhlkY4pip7EU76mRJq6S4FttSfbvBWtZY0BQpa57T3E=";
	public String getPartnerPubkey() {
		return partnerPubkey;
	}
	public void setPartnerPubkey(String partnerPubkey) {
		RSAUtil.partnerPubkey = partnerPubkey;
	}
	public String getSelfPrivateKey() {
		return selfPrivateKey;
	}
	public void setSelfPrivateKey(String selfPrivateKey) {
		RSAUtil.selfPrivateKey = selfPrivateKey;
	}
	
	public static String genSign(String format,String v,String service,String partner,String sec_id,String req_id,String req_data) throws Exception{
		String signString = "format="+format+"&"+"partner="+partner+"&"+"req_data="+req_data+"&"+"req_id="+req_id+"&"+"sec_id="+sec_id+"&"+"service="+service+"&"+"v="+v;
		SytLog.i("签名数据："+signString);
//		byte[] rsaResult = RSACoder.encryptByPrivateKey(signString.getBytes(), selfPrivateKey);
//		return new String(rsaResult);
//		return RSACoder.encryptByPrivateKey(signString.getBytes(), selfPrivateKey);
		String signvalue = RSACoder.sign(signString.getBytes(), selfPrivateKey);
		SytLog.i("签名后数据"+signvalue);
		
		SytLog.i("签名后数据并去掉回车"+signvalue.replace(System.getProperty("line.separator"), ""));
		return signvalue.replace(System.getProperty("line.separator"), "");
	}
	
	public static String encryptReqdata(String reqdata) throws Exception{
//		byte[] rsaResult = RSACoder.encryptByPublicKey(reqdata.getBytes(), partnerPubkey);
//		return new String(rsaResult);
		return  RSACoder.encryptByPublicKey(reqdata.getBytes(), partnerPubkey);
	}
	
	public static boolean verfy(byte[] data, String sign) throws Exception {
		return RSACoder.verify(data, partnerPubkey, sign);
	}
	/**
	 * RSA解密
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String decryptData(byte[] data) throws Exception{
		return RSACoder.decryptByPrivateKey(data, selfPrivateKey);
	}
	
	public static void main(String[] args) throws Exception {
//		System.out.println(RSACoder.encryptByPrivateKey("083724w089yq0".getBytes(), selfPrivateKey));
		String data = "format=xml&partner=2088102011174506&req_data=<?xml version=\"1.0\"?><agency_pre_deposit_req>  <mobile>13917965286</mobile><amount>12</amount><partner_pay_time>2011-12-14 17:01:18</partner_pay_time><partner_bill_no>0120111214001041</partner_bill_no><partner_agent_no>111111</partner_agent_no></agency_pre_deposit_req>&req_id=0120111214000960&sec_id=&service=01&v=1.0";
		String sign = "RFG3iuPOjjNcAENfqs0zjmPP9ZPODlPkTwprb/c78tuLKicoqRJWdMujB0y46g8ZET4iyCMiddm3TbcRcdXpM+2GRjcQOfjlzmthUaYz7+qaj//U0zijDn9hZ2w1x12SpIAlhHIdo/cWQjlVLV/o39xh0+rekS0CYeaW35n7E9Y=";
		System.out.println(RSACoder.verify(data.getBytes(), selfPublicKey, sign));
		
	}
}

